import 'dart:convert';

import 'package:flutter/material.dart';
import '../../widgets/inputWidget.dart';
import '../../model/things.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/someCards.dart';
import '../../helper/Helper.dart';
import '../../helper/uidata.dart';

class ThingsAddPage extends StatefulWidget {
  @override
  ThingsAddPage(this.t1, this.added);
  final Things t1;
  final VoidCallback added;
  @override
  ThingsAddPageState createState() => new ThingsAddPageState();
}

class ThingsAddPageState extends State<ThingsAddPage> {
  //List<String> sList={"一般","重要","紧急"}.toList();
  late Things X1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    loadData();

    //c3.text=X1.Process;
    //c4.text=X1.Workers;
  }

  loadData() async {
    var now = Helper.DateTimeNowString();
    var uGuid = UIData.CurrentUser!.Guid;

    if (widget.t1 != null) {
      X1 = widget.t1;
    }

    if (X1 == null) {
      if (Helper.localDB.containsKey("ThingsData")) {
        var d1 = await Helper.localDB.getString("ThingsData");
        X1 = Things.fromJson(jsonDecode(d1!));
      }
    }

    if (X1 == null) {
      X1 = Things(0, "", "", "", "", "", "");
    }

    c1.text = X1.TName;
    c2.text = X1.TType;
    c3.text = X1.SStyle;
    c4.text = X1.Unit;
    c5.text = X1.Storage;
    c6.text = X1.Remarks;
  }

  upload(BuildContext context) async {
    X1.TName = c1.text;
    X1.TType = c2.text;
    X1.SStyle = c3.text;

    X1.Unit = c4.text;
    X1.Storage = c5.text;
    X1.Remarks = c6.text;

    await Things.Add(X1);
    Helper.localDB.setString("ThingsData", jsonEncode(X1));
    widget.added();
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("新建物品"),
      ),
      body: ThingsAddBody(context),
    );
  }

  ThingsAddBody(BuildContext context) => SingleChildScrollView(
        child: ThingsAddFields(context),
      );

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list3.add(InputWidget.getInput("名称", c1));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("类别", c2));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("规格 型号", c3));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("单位", c4));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("存放地点", c5));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput2("备注", c6));

    list4.add(InputWidget.getButton("提交", () {
      upload(context);
    }));
    list1.add(InputWidget.inCard2(list3));
    list1.add(InputWidget.inCard2(list4));

    return list1;
  }

  ThingsAddFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
